﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GosWeb.Models.Db;

namespace GosWeb.Biz
{
    public class BiontBiz
    {
        static BiontBiz ins = null;
        GosDBContext db = GosDBContext.GetInstance();

        public static BiontBiz GetInstance()
        {
            if (ins == null)
            {
                ins = new BiontBiz();
            }
            return ins;
        }

        public Biont GetCommonAncestor(int layer, string an1, string an2)
        {
            if (an1 == an2)
            {
                return null;
            }

            List<Biont> an1Ancestors = new List<Biont>() { };
            List<Biont> an2Ancestors = new List<Biont>() { };

            Biont a1 = FindBiont(an1);
            Biont a2 = FindBiont(an2);

            if (a1 == null || a2 == null)
            {
                return null;
            }

            an1Ancestors.Add(a1);
            an2Ancestors.Add(a2);
            //var zhuxians = new List<Biont>();

            GetZhuXian(a1, 0, layer, an1Ancestors);
            GetZhuXian(a2, 0, layer, an2Ancestors);

            foreach (var zx1 in an1Ancestors)
            {
                foreach (var zx2 in an2Ancestors)
                {
                    if (zx1.BiontID == zx2.BiontID)
                    {
                        return zx1;
                    }
                }
            }
            return null;
        }

        public Biont FindBiont(string an)
        {
            Biont a = db.Bionts.Find(an);
            return a;
        }

        /// <summary>
        /// 取到祖先列表
        /// </summary>
        /// <param name="an"></param>
        /// <param name="currentLayer"></param>
        /// <param name="maxLayer"></param>
        /// <param name="zhuxian"></param>
        public void GetZhuXian(Biont an, int currentLayer, int maxLayer, List<Biont> zhuxian)
        {
            //List<Biont> la = new List<Biont>();
            if (currentLayer >= maxLayer)
            {
                return;
            }

            if (an != null)
            {
                //var a = an as Biont;
                var fa = an.GetFather();
                if (fa!=null)
                {
                    zhuxian.Add(fa); 
                    GetZhuXian(fa, currentLayer + 1, maxLayer, zhuxian);
                }
                
                var ma = an.GetMother();
                if (ma!=null)
                {
                    zhuxian.Add(ma);
                    GetZhuXian(ma, currentLayer + 1, maxLayer, zhuxian);
                } 
            }
        }

        //public string PedigreeTreeString(BiontNormal bi)
        //{
        //  return  string.Format(
        //    "<ul> {0} </ul>", PedigreeTree(bi));
        //}

        //public string PedigreeTree(Biont bi)
        //{
        //    if (bi == null)
        //    {
        //        return string.Empty;
        //    }

        //    string res;
        //    if (bi.GetType() == typeof(Biont))
        //    {
        //        var a = bi as Biont;

        //        var fa = a.GetFather();
        //        var ma = a.GetMother();
        //        string faTree = PedigreeTree(fa);
        //        string maTree = PedigreeTree(ma);
        //        res = string.Format("<li><a href='/Biont/Details/{0}'> {0}</a> - <span style='color:#229911'>[{3}] </span> - {4} - <span style='color:#444444'>{5}</span> - {6} <ul> {1} {2} </ul> </li>", bi.BiontID, faTree, maTree, bi.GeneRatioStr, bi.SexStr, bi.Address, bi.FromToStr);
        //    }
        //    else
        //    {
        //        res = string.Format("<li><a href='/Biont/Details/{0}'> {0}</a> - <span style='color:#005566'>[{1}] </span> - {2} - <span style='color:#555555'>{3}</span> - {4} </li>", bi.BiontID, bi.GeneRatioStr,  bi.SexStr, bi.Address, bi.FromToStr);
        //    }

        //    return res;
        //}

        public string PedigreeTree(string biId)
        {
            if (string.IsNullOrWhiteSpace(biId))
            {
                return string.Empty;
            }
            else
            {
                var bi = FindBiont(biId);
                string res;
                if (bi == null)
                {
                    res = string.Format("<li><a href='/Biont/Details/{0}'>{0}</a> - <span style='color:red'>未找到</span>,<a  target='_blank' href='/Biont/Create/{0}'>要创建吗</a>?</li>", biId);
                }
                else
                {
                    //var a = bi as Biont;

                    var fa = bi.FatherID;
                    var ma = bi.MotherID;
                    string faTree = PedigreeTree(fa);
                    string maTree = PedigreeTree(ma);
                    res = string.Format("<li><a href='/Biont/Details/{0}'> {0}</a> - <span style='color:#229911'>[{3}] </span> - {4} - <span style='color:#444444'>{5}</span> - {6} <ul> {1} {2} </ul> </li>", bi.BiontID, faTree, maTree, bi.GeneRatioLongStr, bi.SexStr, bi.Address, bi.FromToStr);
                }
                //else
                //{
                //    res = string.Format("<li><a href='/Biont/Details/{0}'> {0}</a> - <span style='color:#005566'>[{1}] </span> - {2} - <span style='color:#555555'>{3}</span> - {4} </li>", bi.BiontID, bi.GeneRatioStr, bi.SexStr, bi.Address, bi.FromToStr);
                //}

                return res;
            }
        }

        public static string GetPrintDetailStr(Biont b)
        {
            string ret = string.Empty;
            if (b != null)
            {
             ret =string.Format("{0}{1}", b.GetParentStr(),b.GetLongID()); 
            }
            return ret;
        }

        public string GetSiteString(string siteCode)
        {
           var v = db.Parameters.Where(m => m.ParaType == (short)ParamType.Site && m.Code == siteCode);
           if (v.Count() == 0)
           {
               return siteCode;
           }
           else
           {
               return v.First().Value;
           }

        }

        public IQueryable<OtherParameter> GetSites()
        {
            var v = db.Parameters.Where(m => m.ParaType == (short)ParamType.Site);
            return v;
        }

        public IQueryable<Gene> Genes
        {
            get
            {
                return GosDBContext.GetInstance().Genes;
            }
        }


    }
}